package com.visionvera.retrofitservice.module.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.RxFragment;
import com.visionvera.retrofitservice.rxbus.RxBus;
import com.visionvera.retrofitservice.rxbus.event.BaseEvent;
import com.visionvera.retrofitservice.view.EmptyLayout;

import butterknife.ButterKnife;
import io.reactivex.functions.Consumer;

/**
 * Created by dongyukun on 2017/8/10.
 * 碎片基类
 */
public abstract class BaseFragment <T extends IBasePresenter> extends RxFragment implements IBaseView, EmptyLayout.OnRetryListener {

    /**
     * 注意，资源的ID一定要一样
     */
    protected EmptyLayout mEmptyLayout;
    protected Context mContext;
    protected T mPresenter;

    //缓存Fragment view
    private View mRootView;
    private boolean mIsMulti = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(attachLayoutRes(), null);
            ButterKnife.bind(this, mRootView);
            registerRxBus();
            initViews();
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mListener.onDestroy();
        unRegisterRxBus(this);
    }

    /**
     * 注册rxBus
     */
    private void registerRxBus() {
        RxBus.getInstanceBus().registerRxBus(BaseEvent.class, new Consumer<BaseEvent>() {
            @Override
            public void accept(BaseEvent baseEvent) throws Exception {
                if (baseEvent != null) {
//                            if (baseEvent instanceof FinishEvent) {
//                                finish();
//                            } else {
                    doRxBus(baseEvent);
//                            }
                }
            }
        });
    }

    /**
     * 销毁rxBus
     * @param o
     */
    private void unRegisterRxBus(Object o) {
        RxBus.getInstanceBus().unSubscribe(o);
    }

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            updateViews(false);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isVisible() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            updateViews(false);
        } else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }

    @Override
    public void showLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showNetError() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
            mEmptyLayout.setRetryListener(this);
        }
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void onRetry() {
        updateViews(false);
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.<T>bindToLifecycle();
    }

    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    protected abstract int attachLayoutRes();

    /**
     * 初始化视图控件
     */
    protected abstract void initViews();

    /**
     * 更新视图控件
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    protected abstract void updateViews(boolean isRefresh);

    /**
     * 处理RxBusEvent事件
     *
     * @param baseEvent
     */
    protected abstract void doRxBus(BaseEvent baseEvent);

    /**
     * 回调函数
     */
    public LifeCycleListener mListener;

    public void setOnLifeCycleListener(LifeCycleListener listener) {
        mListener = listener;
    }

}
